<?php
/**
 * Special price list admin filter
 *
 * @author Lukas Normantas
 */
class Catalog_Model_SpecialAdminFilter extends Areal_Model_CollectionFilter
{
    public function filterQuery(Doctrine_Query $query)
    {
        $data = self::getData();
        if ($data['interval_start']){

            $query->addWhere( 's.end_at >= ?' ,  $data['interval_start'] );
        }
        if ($data['interval_end']){
            $query->addWhere( 's.start_at <= ?' ,  $data['interval_end'] );
        }
        $query->orderBy($this->getSort() );
        return $query;
    }

    protected $_form = null;

    public function getForm()
    {
        if ($this->_form == null){
            $this->_form = new Catalog_Form_SpecialAdminList();
            $this->_form->setDefaults( $this->getData() );
        }
        return $this->_form;
    }

    public function getDefaultSort()
    {
        return 's.created_at';
    }

    public function getDefaultData()
    {
        return array( 'interval_start' => date('Y-m-d'), 'interval_end' => date('Y-m-d'),);
    }

    public function getAdminPaginator($currentPage)
    {
        $resultsPerPage = 10;
        $q = Doctrine_Query::create()
           ->select('s.*, p.*')
           ->from( 'Catalog_Model_Special s' )
           ->leftJoin( 's.Product p' );
        $this->filterQuery($q);
        $doctrinePager = new Doctrine_Pager( $q, $currentPage, $resultsPerPage );
        $zendPaginator = Areal_Paginator::build($doctrinePager);
        return $zendPaginator;
    }
}